<?php
/**
 * View Nuovo Post - LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    21-apr-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script type="text/javascript" src="/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/js/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript" src="/js/ckeditor/load-editor.js"></script>
<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {

	var validator = $("#locationForm").validate({
	    invalidHandler: function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
		    $("li#errorLi").show();
		} else {
		    $("li#errorLi").hide();
		};
	    },
	    
	    // Assegna classe 'error' al <li> dell'elemento invalido
	    errorPlacement: function(error, element) {
		//console.log(element);
		element.parent("span").parent("div").parent("li").addClass("error");
		error.appendTo( element.parent("span").parent("div"));
	    },
	    submitHandler: function(form) {
		//$('#salva').attr('disabled', 'disabled');
		form.submit();
	    },	    
	    errorElement: "p",
	    groups: {
		address: "location_street location_street location_street_number location_loc location_prov location_cap"
	      },
	    rules: {
		location_name:{
		    required: true
		},
		location_street: {
		    required: true
		},
		location_city: {
		    required: true
		},
		location_cap:{
		    digits: true,
		    rangelength: [5, 5]
		}
	    },
	    messages: {
		location_name: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		location_street: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		location_city: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		location_cap: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'CAP' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'CAP' )?>"
		}
	    },
	    debug:true
	});

	// Risolve un bug d'integrazione tra jQuery e CKeditor
	$("#salva").click(function(){
			CKEDITOR.instances.location_desc.updateElement();
		});

	$( '#location_desc' ).ckeditor(function(){}, ckeditorConf );
	
	$('#location_desc').val('<?php echo  (addslashes($this->data['location_desc']))?>');

    });
    
    $("#salva").click(function() {
      validator.form();
    });
    
    //]]>
</script>

<div class="info">
    <h2>Inserisci Nuovo Post</h2>
    <div>Per inserire un nuovo post...</div>
    <div>I campi contrassegnati da <span class="req">*</span> sono obbligatori!</div>
</div>
<!-- This <div> holds alert messages to be display in the sample page. -->
<div id="alerts">
    <noscript>
    <p>
	<strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
				support, like yours, you should still see the contents (HTML data) and you should
				be able to edit it normally, without a rich editor interface.
    </p>
    </noscript>
</div>
<form class="fab" name="locationForm" id="locationForm" action="/admin/location/edit/id/" method="post" enctype="multipart/form-data">
    <ul>
	<li id="errorLi" style="<?php echo $this->errorMsg ? '' : 'display:none'?>">
            <h3 id="errorMsgLbl">Si &egrave; verificato un problema durante l'invio del modulo.</h3>
	    <p id="errorMsg">
		Di seguito trovi evidenziati gli errori riscontrati.
	    </p>
	</li>
	
	<li class="<?php echo $this->errorMsg['location_name'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['location_name'] ? 'error' : ''?>">Nome Locale<span class="req">*</span></label>
	    <div>
		<span>
		    <input id="location_name" name="location_name" class="field text small" type="text" maxlength="255" value="<?php echo $this->data['location_name']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['location_name'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['location_name']?></p>
	</li>
	
	<li class="complex <?php echo $this->errorMsg['location_address'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['location_address'] ? 'error' : ''?>">Indirizzo<span class="req">*</span></label>
	    <div>
		<span class="input">
		    <input class="field text addr" name="location_street" id="location_street" size="50" type="text" value="<?php echo $this->data['location_street']?>" />
		    <label>Via</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_street_number" id="location_street_number" size="2" type="text" value="<?php echo $this->data['location_street_number']?>" />
		    <label>N&deg;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_loc" id="location_loc" size="50" type="text" value="<?php echo $this->data['location_loc']?>" />
		    <label>Localit&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_city" id="location_city" size="50" type="text" value="<?php echo $this->data['location_city']?>" />
		    <label>Citt&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_cap" id="location_cap" size="10" type="text" value="<?php echo $this->data['location_cap']?>" />
		    <label>CAP</label>
		</span>

		<span class="left">
		    <select class="field select addr" name="location_province_id" id="location_province_id">
			<?php foreach( $this->province AS $k => $provincia ): ?>
			    <option value="<?php echo $k?>" <?php echo ( $this->data['location_province_id'] == $k ) ? 'selected="selected"' : ''?> ><?php echo $provincia['province_long_name']?></option>
			<?php endforeach; ?>
		    </select>
		    <label>Provincia</label>
		</span>
		<p class="error" style="<?php echo $this->errorMsg['location_address'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['location_address']?></p>
	    </div>
	</li>	
	
	<li class="section">
	    <h3></h3>
	</li>	
	
	<li class="<?php echo $this->errorMsg['location_desc'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['location_desc'] ? 'error' : ''?>">Info Locale</label>
	    <div>
		<span>
		    <textarea id="location_desc" name="location_desc" rows="5" cols="10"><?//=  htmlentities($this->data['post_text'])?></textarea>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['location_desc'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['location_desc']?></p>
	</li>
	<input id="id" name="id" type="hidden" value="<?php echo $this->data['location_id']?>" />
    </ul>
    <div id="formButtons">
	<button id="salva" type="submit" name="salva" value="salva" class="button positive">
	    <img src="/images/admin/tick.png" alt="" />Salva
	</button>
    </div>
</form>
